package com.fingard.dsp.bank.directbank.cdrcb01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CDRCB01SinglePay extends CDRCB01Base implements ISinglePay {
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);

		if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
			tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	// 1908
	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		
		CDRCB01Header bankHeader = new CDRCB01Header();

		if (p_req1908.singleDetail.sameBnk.equals("1")) {
			bankHeader.tranCode = "020102";// 行内（同行）
		} else {
			bankHeader.tranCode = "020103";// 行外（跨行）
		}
		XmlTextWriter xmlWriter = new XmlTextWriter();
		xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
		XElement tmpXElement = new XElement("BCDL", "");
		tmpXElement.addChild(new XElement("CST_FLOW", p_req1908.singleDetail.askNumber));// 流水号
		tmpXElement.addChild(new XElement("PAY_ACCT", p_req1908.singleDetail.payAct));// 支付账号
		tmpXElement.addChild(new XElement("REC_ACC", p_req1908.singleDetail.recAct));// 接收账号
		tmpXElement.addChild(new XElement("REC_NAME", p_req1908.singleDetail.recName));// 接收名字
		String isPrivate = p_req1908.singleDetail.isPrivate.equals("1") ? "2" : "";
		tmpXElement.addChild(new XElement("REC_TYPE", isPrivate));// 收款类型
		tmpXElement.addChild(new XElement("TRAN_AMT", p_req1908.singleDetail.payAmount));// 交易金额
		tmpXElement.addChild(new XElement("TRAN_CRY", this.convertCurToBank(p_req1908.singleDetail.payCur)));// 交易币种
		// 加急判断
		String paySpd = p_req1908.singleDetail.paySpd;
		String priority = "";
		if (paySpd.equals("0")) {
			priority = "01";
		} else if (paySpd.equals("1")) {
			priority = "02";
		}
		if (bankHeader.tranCode.equals("020102")) {// 行内
			tmpXElement.addChild(new XElement("REC_BANK", ""));// 收款款行号
			tmpXElement.addChild(new XElement("PRIORITY", priority));// 加急标识
		} else// 行外
		{
			if (priority.length() == 0)
				priority = "01";
			String bankId = p_req1908.singleDetail.cnapsCode;
			if (bankId.equals("-1")) {
				throw new Exception("未找到相应收款方行号");
			}
			tmpXElement.addChild(new XElement("REC_BANK", bankId));// 收款款行号(一定)
			tmpXElement.addChild(new XElement("PRIORITY", priority));// 加急标识
		}

		String purpose = "";
		// 收款方类型为2(个人)时，用途一栏央行相应的规定的用途编号，列表见数据字典“个人收款备注”
		if (isPrivate.equals("2")) {
			purpose = getBankConfig().getMapValueByKey("PURPOSE1908", p_req1908.singleDetail.payType);
			if (purpose.length() == 0) {
				throw new Exception("未找到相应用途");
			}
		} else {
			purpose = p_req1908.singleDetail.payType;
		}
		tmpXElement.addChild(new XElement("PURPOSE", purpose));// 收款用途
		tmpXElement.addChild(new XElement("MAK1", ""));
		nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
		xmlWriter.writeXElement(tmpXElement);
		xmlWriter.writeEndDocument();
		System.out.println(xmlWriter.toXmlString());

		String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader, "ALL");// 发送银行
		// String[] tmpStrRet = { "", "" };
		// tmpStrRet[0] = "";
		// tmpStrRet[1] = "00000000|success|20150803102541380#<?xml
		// version=\"1.0\" encoding=\"UTF-8\"
		// ?><BCDL><RET_CODE>000</RET_CODE><RET_MSG>交易成功</RET_MSG><TRAN_STT>90</TRAN_STT><CST_FLOW>201508031021138921002</CST_FLOW><BANK_FLOW>1C5080310343391</BANK_FLOW><FEE/></BCDL>";

		// 处理银行返回的数据
		if (tmpStrRet[0].length() == 0) {
			String[] strRet = splitRetToString(tmpStrRet[1]);// 把结果拆分成String数组
			if (strRet[0].equals("00000000") && strRet[1].equals("success")) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(strRet[3], tmpSbMsg);
				if (tmpBkReader != null) {
					if (tmpBkReader.read("/BCDL")) {
						String ret_code = tmpBkReader.readedXElement.getSingleText("RET_CODE");
						String tranStt = tmpBkReader.readedXElement.getSingleText("TRAN_STT");
						
						retDetail.respCode = "0";
						retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("RET_MSG");
						retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("CST_FLOW");
						retDetail.transState = ret_code + "+" + tranStt;
						retDetail.payInfoCode = retDetail.respInfo;
					} else { // 处理失败respCode返回为-1
						retDetail.respCode = "-1";
						retDetail.respInfo = "银行返回内容格式不正确";
					}
				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = strRet[1];
			}
		} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
			retDetail.respCode = "0";
			retDetail.respInfo = tmpStrRet[1];
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = tmpStrRet[0];
		} else {
			retDetail.respCode = "-1";
			retDetail.respInfo = tmpStrRet[1];
		}
		return retDetail;
	}

	// 1944
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {

		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		CDRCB01Header bankHeader = new CDRCB01Header();
		bankHeader.tranCode = "001005";
		XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
		xmlWriter.autoFormat = false;
		xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
		xmlWriter.writeStartElement("BCDL");
		xmlWriter.writeElementString("TRAN_FLAG", "1");
		xmlWriter.writeElementString("CST_FLOW", p_req1944.askNumber);
		xmlWriter.writeEndDocument();

		// System.out.println(xmlWriter.toXmlString());
		try {
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader, "ALL");// 发送银行
			// String[] tmpStrRet = { "", "" };
			// tmpStrRet[0] = "";
			// tmpStrRet[1] = "00000000|success|20150803102752515#<?xml
			// version=\"1.0\" encoding=\"UTF-8\"
			// ?><BCDL><RET_CODE>000</RET_CODE><RET_MSG>交易成功</RET_MSG><TRAN_STT>90</TRAN_STT><CST_FLOW>201508031021138921002</CST_FLOW><BANK_FLOW>1C5080310343391</BANK_FLOW></BCDL>";

			if (tmpStrRet[0].length() == 0) {
				String[] strRet = splitRetToString(tmpStrRet[1]);// 把结果拆分成String数组
				if (strRet[0].equals("00000000") && strRet[1].equals("success")) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(strRet[3], tmpSbMsg);
					if (tmpBkReader != null) {
						if (tmpBkReader.read("/BCDL")) {
							String ret_code = tmpBkReader.readedXElement.getSingleText("RET_CODE");
							String tranStt = tmpBkReader.readedXElement.getSingleText("TRAN_STT");
							
							retDetail.respCode = "0";
							retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("RET_MSG");
							retDetail.transState = ret_code + "+" + tranStt;
							retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("CST_FLOW");
							retDetail.payInfoCode = retDetail.respInfo;
						} else { 
							retDetail.respCode = "-1";
							retDetail.respInfo = "返回报文格式不正确";
						}
					} else {
						retDetail.respCode = "-1";
						retDetail.respInfo = tmpSbMsg.toString();
					}
				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = strRet[1];
				}

			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception e) {
			retDetail.respCode = "-1";
			retDetail.respInfo = e.getMessage();
			e.printStackTrace();
		}
		
		retAts.writeRetDetail(retDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
